Repository navigation
fix(api): serve read-cache hits without raising through the app - #2006
Merged
Merged
Conversation
fastapi-redis-sdk signals a cache hit by raising CacheHitException from its dependency. Raised while FastAPI solves dependencies, Logfire's FastAPI instrumentation records it on the request span, so every cache hit showed up as an exception (about 76k spans a day in Cloud). ReadCacheRoute now runs the SDK lookup in the route handler, before dependency solving, catches the hit there and returns the cached response. cached_read stays the route-level marker and fails fast if its router does not use ReadCacheRoute. Keys, ETag/304, hit headers, capture, invalidation and the read_cache.read_through span are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016N9c6ouiT4Wid3arGR41Dv Signed-off-by: phernandez <paul@basicmachines.co>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
fastapi-redis-sdk (0.8.1) signals a cache hit by raising
CacheHitExceptionfrom itscache()dependency, and an exception handler turns it into the cached response. The raise happens while FastAPI solves dependencies, and Logfire's FastAPI instrumentation records any exception that leaves dependency solving on the request span. So every read-cache hit was recorded as an exception. In Basic Memory Cloud production that is about 76k spans a day withexception_type=redis_fastapi.cache.CacheHitExceptionon GETknowledge/entities/{id},directory/structure,directory/list,directory/tree, QUERYknowledge/resolve, search andmemory/recent.A cache hit is not an error. The earlier change that raised the hit after the
read_cache.read_throughspan closed kept it off that span, but not off the request span.The SDK has no non-exception hit mode: a FastAPI dependency cannot return a response, so the only way out of dependency solving is to raise.
What
ReadCacheRoute(anAPIRoutesubclass) inread_cache/routes.py. For a route with acached_readdependency, its handler runs the SDK lookup before FastAPI solves dependencies. On a hit it catchesCacheHitExceptionright there and returns the cached response. On a miss it runs the normal handler and the SDK capture middleware stores the response as before.cached_read(...)is still declared asdependencies=[cached_read(...)]. It now carries the route's cache settings, and when FastAPI calls it, it only checks thatReadCacheRouteserved the request. It raisesTypeErrorif a router forgotroute_class=ReadCacheRoute, so a route cannot silently run uncached.route_class=ReadCacheRoute.X-Redis-Cache, capture, entry tracking, invalidation, and theread_cache.read_throughspan withcache.lookup.outcome. A hit now also returns before any dependency runs at all, not just before database dependencies.logfirebecomeslogfire[fastapi]so tests can instrument the app the way production does (addsopentelemetry-instrumentation-fastapito the dev lock only).No monkeypatching of the SDK. The SDK's exception handler is still registered by
add_redis_cachingbut is no longer reached.Testing
test_a_cache_hit_is_not_recorded_as_an_exception_on_the_request_span(test-int, real Redis): instruments the app withlogfire.instrument_fastapi, does a miss then a hit, checks the hit returns the cached body and no span carries an exception event.src/change reverted, it fails with['redis_fastapi.cache.CacheHitException'] == []. With the fix it passes.tests/test_read_cache_route_class.py: acached_readroute works underReadCacheRouteand fails fast without it.tests/test_read_cache_*.pyandtest-int/read_cache/: 102 passed.tests/api/v2: 460 passed, 1 skipped.just fast-check(ruff, format, ty) clean; pyright clean on touched files.🤖 Generated with Claude Code
https://claude.ai/code/session_016N9c6ouiT4Wid3arGR41Dv